package com.it.web.servlet;


import com.alibaba.fastjson.JSON;
import com.it.pojo.Brand;
import com.it.pojo.PageBean;
import com.it.service.BrandService;
import com.it.service.impl.BrandServiceImpl;
import org.apache.ibatis.annotations.ResultMap;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@WebServlet("/brand/*")
public class BrandServlet extends baseServlet{

    private BrandService brandservice = new BrandServiceImpl();

    //设置查询全部方法
    public void selectAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        System.out.println("brand all ...");
        // 调用service
        List<Brand> brands = brandservice.selectAll();

        // 转为JSON
        String s = JSON.toJSONString(brands);

        //写数据
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(s);
    }

    public void addServlet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
//        System.out.println("brand add ...");

        BufferedReader reader = request.getReader();
        String s = reader.readLine(); //json字符串

        //json转brand对象
        Brand brand = JSON.parseObject(s, Brand.class);
        //执行sql
        brandservice.add(brand);
        //响应成功表示
        response.getWriter().write("success");
    }

    public void deleteBytes(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
//        System.out.println("brand add ...");

    BufferedReader reader = request.getReader();
    String s = reader.readLine(); //json字符串

    //json转int数组对象
    int[] ints = JSON.parseObject(s, int[].class);

    //执行sql
        brandservice.deleteByIds(ints);
    //响应成功表示
        response.getWriter().write("success");
}
    public void selectPageBean(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //当前页码 每页条数
        // url?currentPage=1&pageSize=5

        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");

        int currentPage = Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_pageSize);


        PageBean<Brand> brandPageBean = brandservice.selectByPage(currentPage, pageSize);

        // 转为JSON
        String s = JSON.toJSONString(brandPageBean);

        //写数据
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(s);
    }

    public void selectPageBeanAndCondition(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //当前页码 每页条数
        // url?currentPage=1&pageSize=5
        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");


        int currentPage = Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_pageSize);

        //获取条件查询对象
        BufferedReader reader = request.getReader();
        String s1 = reader.readLine(); //json字符串

        //转为brand
        Brand brand = JSON.parseObject(s1, Brand.class);


        PageBean<Brand> brandPageBean = brandservice.selectByPageAndCondition(currentPage,pageSize,brand);



        // 转为JSON
        String s = JSON.toJSONString(brandPageBean);

        //写数据
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(s);
    }
}
